package android.slc.appbase.ui.dialog;

import android.slc.box.commonlibrary.compat.SlcResourceUtils;
import io.reactivex.rxjava3.annotations.NonNull;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 快速对话框工具
 */
public class SlcFastDialogUtils {

    public static void applyStylesheets(Dialog<?> dialog) {
        dialog.getDialogPane().getStylesheets().add("/style/app.css");
    }

    public static boolean btnIsOk(@NonNull ButtonType buttonType) {
        return btnIsTarget(buttonType, ButtonType.OK);
    }

    public static boolean btnIsCancel(@NonNull ButtonType buttonType) {
        return btnIsTarget(buttonType, ButtonType.CANCEL);
    }

    public static boolean btnIsTarget(@NonNull ButtonType buttonType, @NonNull ButtonType target) {
        return target.equals(buttonType) || target.getButtonData().equals(buttonType.getButtonData());
    }

    public static Builder createPrompt(@NonNull String contentText) {
        return createBuilder().setContentText(contentText).setPositiveText(null).setAlertType(Alert.AlertType.INFORMATION);
    }

    public static void showPrompt(@NonNull String contentText) {
        createPrompt(contentText).build().show();
    }

    public static Builder createBuilder() {
        return new Builder();
    }

    public static class Builder {
        private Alert.AlertType alertType = Alert.AlertType.CONFIRMATION;
        private String title = SlcResourceUtils.getString("label_prompt");
        private String contentText;
        private String positiveText = SlcResourceUtils.getString("action_ok");
        private String negativeText = SlcResourceUtils.getString("action_close");

        public Builder setAlertType(Alert.AlertType alertType) {
            this.alertType = alertType;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentText(String contentText) {
            this.contentText = contentText;
            return this;
        }

        public Builder setPositiveText(String positiveText) {
            this.positiveText = positiveText;
            return this;
        }

        public Builder setNegativeText(String negativeText) {
            this.negativeText = negativeText;
            return this;
        }

        public Alert build() {
            Alert alert = new Alert(alertType);
            SlcFastDialogUtils.applyStylesheets(alert);
            alert.setHeaderText(null);
            if (StringUtils.isNotEmpty(title)) {
                alert.setTitle(title);
            }
            alert.setContentText(contentText);
            List<ButtonType> buttonTypeList = new ArrayList<>();
            if (StringUtils.isNotEmpty(positiveText)) {
                ButtonType positiveButtonType = new ButtonType(positiveText, ButtonType.OK.getButtonData());
                buttonTypeList.add(positiveButtonType);
            }
            if (StringUtils.isNotEmpty(negativeText)) {
                ButtonType negativeButtonType = new ButtonType(negativeText, ButtonType.CANCEL.getButtonData());
                buttonTypeList.add(negativeButtonType);
            }
            alert.getButtonTypes().setAll(buttonTypeList);
            return alert;
        }
    }

}
